package com.mz.ozy.widget.ob;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.ViewPager;


import com.mz.ozy.R;

import java.lang.reflect.Field;


/**
 * Created by Nowy on 2018/1/9.
 * 填充一页高度的ViewPager
 * 高度：ViewPager高度 = 屏幕高度 - 标记控件的高度 - mRemoveHeight - StatusBarHeight
 * 高度偏移量:mHeightOffset = 标记控件的高度 + mRemoveHeight + StatusBarHeight
 * app:targetId，设置统一屏幕显示的ViewID。
 * app:removeHeight 扣除的偏移量
 */

public class MatchViewPager extends ViewPager {
    public static final String TAG = MatchViewPager.class.getSimpleName();
    private int mTargetId;//标记ID,ViewPager高度 = 屏幕高度 - 标记控件的高度
    private int mRemoveHeight;//其他偏移量，可能需要减去标题栏的高度
    private int mHeightOffset = 0;//
    private int mTargetViewHeight;

    public MatchViewPager(Context context) {
        super(context);
        initHeight();
    }

    public MatchViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        initHeight();
    }


    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MatchViewPager);

        mTargetId = a.getResourceId(R.styleable.MatchViewPager_targetId, 0);

        mRemoveHeight = a.getDimensionPixelOffset(R.styleable.MatchViewPager_removeHeight, 0);
        a.recycle();
    }

    private void initHeight() {
        post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = calcHeight() + 1;
                setLayoutParams(params);
            }
        });

    }


//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//
////        int height = calcHeight() + 1;
////        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//    }


    private int calcHeight() {
        ViewGroup parent = (ViewGroup) getParent();
        int height = DeviceUtil.getDeviceHeight(getContext()) - getStatusBarHeight(getContext());
        if (parent != null && mTargetId > 0) {
            View targetView = parent.findViewById(mTargetId);
            if (targetView != null) {
                mTargetViewHeight = targetView.getHeight();
                if (mTargetViewHeight <= 0) {
                    mTargetViewHeight = getViewHeight(targetView);
                }

                height = height - mTargetViewHeight;
            }
        }

        height = height - mRemoveHeight;//扣除偏移量
        return height;
    }


    /**
     * 获取view的高度
     *
     * @param view
     * @return
     */
    public static int getViewHeight(View view) {
        measureView(view);
        return view.getMeasuredHeight();
    }

    /**
     * 测量view
     *
     * @param view
     */
    public static void measureView(View view) {
        ViewGroup.LayoutParams p = view.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight, View.MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        }
        view.measure(childWidthSpec, childHeightSpec);
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Class<?> c;
        Object obj;
        Field field;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(x);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return x;
    }


    public void setTargetId(int mTargetId) {
        this.mTargetId = mTargetId;
    }

    public void setRemoveHeight(int removeHeight) {
        this.mRemoveHeight = removeHeight;
    }

    public int getHeightOffset() {
        return mHeightOffset = (mRemoveHeight + mTargetViewHeight + getStatusBarHeight(getContext()));
    }

}
